<?php
if ( !defined( "IN_OP" ) )
{
    exit( "You can't access this file directly..." );
}
include_once( "class_6h/class.lotto6h.php" );
$lotto6h = new lotto6h();
$bettype = 1;
$ui = new UI();
$tpl = new Template2();
$tpl->load( array( "main" => "proc2.htm" ) );
$tpl->set( "account", $curruser['account'] );
if ( $curruser['enabled'] == 0 || $curruser['enabled'] == 2 )
{
    $ui->showErrorPage( "<p class=error>帐号已被暂停使用</p>" );
    exit();
}
$rs = getdb( "select * from lotto where open>='".date( "Y-m-d" )."' order by open" );
if ( $rs->eof )
{
    $ui->showErrorPage( "<p class=error>目前没有可以下注期数！</p>" );
    exit();
}
$open = $rs->f['open'];
$round = $rs->f['round'];
$lotto_id = $rs->f['id'];
$conf = new Config();
$stopbet = $conf->get( "othebet" );
$prot_start = $conf->get( "protect_start" );
if ( date( "Y-m-d" ) < $open )
{
    exit();
}
else if ( date( "Y-m-d" ) == $open )
{
    if ( $stopbet <= date( "Hi" ) )
    {
        $ui->showErrorPage( "<p>下注期数：".d3( $round )."<br>开奖日：".$open."</p>"."<p class=error>已经截止下注！</p>" );
        exit();
    }
    else if ( date( "Hi" ) <= $conf->get( "startbet" ) )
    {
        exit();
    }
}
$tpl->set( "open", $open );
$tpl->set( "round", d3( $round ) );
$tpl->set( "lotto_id", $lotto_id );
$tpl->set( "account", $curruser['account'] );
$tpl->set( "amount", $curruser['amount'] );
$user_rate0 = get_share_rate0( $curruser['id'] ) - 0;
$rate0_8num = get_share_rate0_8( $curruser['id'] ) - 0;
$user_rate8 = $user_rate0 * $rate0_8num;
$rs1 = getdb( "select amount_6h from k_user_bet where user_id=".$curruser['id']." and lotto_id=".$lotto_id );
$usedamount = $rs1->f[0] ? $rs1->f[0] : 0;
$rs1->close();
$tpl->set( "usedamount", $usedamount );
$bet1 = 0;
$bet2 = 0;
$rs1 = getdb( "select sum(amount) as amount,numbers from bet where bettype=".$bettype." and user_id=".$curruser['id']." and lotto_id=".$lotto_id." and rame=0 group by numbers" );
while ( !$rs1->eof )
{
    if ( $rs1->f['numbers'] == 1 )
    {
        $bet1 += $rs1->f['amount'];
    }
    else if ( $rs1->f['numbers'] == 2 )
    {
        $bet2 += $rs1->f['amount'];
    }
    else
    {
        echo "错误的注单！";
    }
    $rs1->movenext();
}

//$betamount = $bet2;修改
if ( $num == 1 )
{
    $betamount = $bet1;
}
else if ( $num == 2 )
{
    $betamount = $bet2;
}
$tpl->set( "betamount", $betamount );
$num = intval( $_GET['num'] );
if ( $num == 0 )
{
    write_exceptional_log( $curruser['id'], $curruser['acl'], $lotto_id, "使用外挂攻击" );
    err( "不要随便攻击本站！你的 IP 已经被记录了！" );
}
$tpl->set( "num", $num );
$idisk = getUserValidDisk( $curruser );
$disk = $g_disk_code[$idisk];
$tpl->set( "diskStr", strtoupper( $disk )."盘" );
$rs = getdb( "select r1_".$num." from rate1".$disk." where lotto_id={$lotto_id}" );
if ( $rs->eof )
{
    err( "系统目前维护中，请稍後！" );
}
$sub_rates = get_subs_rates( $curruser['id'], $lotto_id, $bettype, $num );
$rate = $rs->f[0] - $user_rate8 - $sub_rates - 0;
$tpl->set( "rate", trimfloat( $rate, 1 ) );
$betmin = $conf->get( "betmin" );
$tpl->set( "min", $betmin );
$betmax = $conf->get( "betmax" );
$tpl->set( "max", $betmax );
$tpl->set( "max_str", $betmax );
$betname = $num == 1 ? "特单" : "特双";
$tpl->set( "betname", $betname );
$rs1 = getdb( "select * from amounts where bettype={$bettype} and user_id=".$curruser['id'] );
$amount1 = $rs1->f['amount1'] - 0;
$amount2 = $rs1->f['amount2'] - 0;
$betallow = $amount2;
/* if ( $num == 1 )
{
    if ( $bet2 < $bet1 )
    {
        $betallow = $amount2 - ( $bet1 - $bet2 );
    }
}
else if ( $num == 2 && $bet1 < $bet2 )
{
    $betallow = $amount2 - ( $bet2 - $bet1 );
} */
//新改
 if ($num == 1 )
{
        $betallow = $amount2 - $bet1;
}
else if ($num == 2)
{
    $betallow = $amount2 - $bet2;
} 
$tpl->set( "amount1", $amount1 );
$tpl->set( "amount2", $amount2 );
$tpl->set( "amount1_str", $amount1 );
$tpl->set( "amount2_str", $betallow );
$usemaxamount = $curruser['usemaxamount'];
$oneamount = $amount1;
$tpl->set( "oneamount", "<tr><td>单注限额：</td> <td align=right><b>".$oneamount."</b></td>" );
if ( ispost( ) )
{
    $money = intval( $_POST['money'] );
    if ( $money == 0 )
    {
        write_exceptional_log( $curruser['id'], $curruser['acl'], $lotto_id, "使用外挂攻击" );
        err( "不要随便攻击本站！你的 IP 已经被记录了！" );
    }
    if ( $money < $betmin )
    {
        err( "下注金额不能小於最低限额！" );
    }
    if ( $amount1 < $money)//修改 原来 $money + $betamount
    {
        err( "下注金额不能超过单项限额！" );
    }
    if ( $curruser['balance'] < $money)//修改 原来 $money + $usedamount
    {
        err( "下注金额不能超过信用额度！" );
    }
     if ( $amount2 < $money)//修改 原来 $money + $usedamount
    {
        err( "下注金额不能超过本期限额！" );
    }
	 if ( $betallow < $money)//修改 原来 $money + $usedamount
    {
        err( "下注金额不能超过下注限额！" );
    }
	
    if ($oneamount <$money)
    {
        err( "下注金额不能超过单注限额！" );
    }
    $payoff = round( $money * ( $rate - 1 ) );
    $stime = systime();
    $fields = array();
    $fields['user_id'] = $curruser['id'];
    $fields['lotto_id'] = $lotto_id;
    $fields['createtime'] = $stime;
    $fields['bettype'] = $bettype;
    $fields['numbers'] = "'{$num}'";
    $fields['amount'] = $money;
    $fields['rate'] = $rate;
    $fields['payoff'] = $payoff;
    $fields['disk'] = $idisk;
    $fields['line_nums'] = "'{$num}'";
    $fields['unitmoney'] = $money;
    $u2 = get_user( $curruser['prev_id'] );
    $u3 = get_user( $u2['prev_id'] );
    $u4 = get_user( $u3['prev_id'] );
    $u5 = get_user( $u4['prev_id'] );
    if ( $conf->get( "stopbet" ) <= date( "Hi" ) )
    {
        err( "已经截止下注！" );
    }
    $fields['acl2'] = $u2['id'];
    $fields['acl3'] = $u3['id'];
    $fields['acl4'] = $u4['id'];
    $fields['acl5'] = $u5['id'];
    $a1 = GetBack( $curruser['id'], $fields['bettype'], $idisk, 1 );
    $a2 = GetBack( $u2['id'], $fields['bettype'], $idisk, 2 );
    $a3 = GetBack( $u3['id'], $fields['bettype'], $idisk, 3 );
    $a4 = GetBack( $u4['id'], $fields['bettype'], $idisk, 4 );
    $a5 = GetBack( $u5['id'], $fields['bettype'], $idisk, 5 );
    $fields['back1'] = $a1;
    $fields['back2'] = $a2;
    $fields['back3'] = $a3;
    $fields['back4'] = $a4;
    $fields['back5'] = $a5;
    $a6 = GetBack( $u5['supe_account'], $fields['bettype'], $idisk, 255 );
    $fields['back6'] = $a6;
    $protmod = $u5['protmod'];
    $fields['mpercent'] = $u2['mpercent'];
    $fields['percent2'] = $u2['percent0'];
    $fields['percent3'] = min( $u3['percent1'], $u4['mpercent'] - $fields['mpercent'] - $fields['percent2'] );
    $fields['percent4'] = min( $u4['percent1'], $u5['mpercent'] - $fields['mpercent'] - $fields['percent2'] - $fields['percent3'] );
    $fields['upercent'] = 100 - $fields['mpercent'] - $fields['percent2'] - $fields['percent3'] - $fields['percent4'];
    if ( $fields['percent3'] < 0 || $fields['percent4'] < 0 || $fields['upercent'] < 0 )
    {
        err( "占成参数设置错误，注单无法存档！" );
        return;
    }
    $fields['r2'] = $money * $fields['percent2'] / 100;
    $fields['r3'] = $money * $fields['percent3'] / 100;
    $fields['r4'] = $money * $fields['percent4'] / 100;
    $fields['r6'] = $money * $fields['upercent'] / 100;
    if ( intval( $conf->get( "reat0_open" ) ) == 1 && $u5['reat0_open'] == 1 )
    {
        $fields['rate1'] = $curruser['rate0'] * $rate0_8num;
        $fields['rate2'] = $u2['rate0'] * $rate0_8num;
        $fields['rate3'] = $u3['rate0'] * $rate0_8num;
        $fields['rate4'] = $u4['rate0'] * $rate0_8num;
    }
    $fields['rate5'] = $sub_rates;
    $fields['ip'] = "'".getIP( )."'";
    $newid = db_insert( "bet", $fields );
    if ( !$newid )
    {
        err( "超过信用额度" );
    }
	$rs=getdb("select user_id from k_user_bet where user_id='".$curruser['id']."' and lotto_id='$lotto_id'");
	if(!$rs->eof)
		getdb("update k_user_bet set amount_6h=amount_6h+".$money." where user_id='".$curruser['id']."' and lotto_id='$lotto_id'");
	else
		getdb("insert into k_user_bet set amount_6h=amount_6h+".$money." ,user_id='".$curruser['id']."',lotto_id='$lotto_id'");
    check_autorate( $lotto_id, $bettype, $num, $money * ( $fields['upercent'] / 100 ) );
    $lotto6h->getBet_6h( $newid );
    $tpl->load( "succ", "succ.htm" );
    $tpl->set( "ordertype", "<font color=red>".$betname."</font> @ <font color=red><b>".trimfloat( $rate, 1 )."</b></font>" );
    $tpl->set( "money", $money );
    $tpl->set( "payoff", $payoff );
    $tpl->show( "succ" );
}
else
{
    if ( ispost( ) )
    {
        $money = intval( $_POST['money'] );
        $tpl->set( "money", $money );
        $tpl->set( "message", "<tr><td colspan=2 bgcolor=red align=center><font color=white>赔率已经变动！</font></td></tr>" );
    }
    else
    {
        $oldrate = $_GET['rate'] - 0;
        if ( $rate != $oldrate )
        {
            $tpl->set( "message", "<tr><td colspan=2 bgcolor=red align=center><font color=white>赔率已经变动！".intval( $rate - $oldrate )."</font></td></tr>" );
        }
    }
    $tpl->show( "main" );
}
?>